package com.xlinur.android.moviecatalog;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.TypedValue;
import android.view.View;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;

public class MovieView extends Activity {
	protected static int[] mTextFields;
	protected static int mTextFieldsLength;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.movie_view);
		Button buttonClose = (Button) findViewById(R.id.button_close);
		buttonClose.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});
		Button buttonEdit = (Button) findViewById(R.id.button_edit);
		buttonEdit.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent editorActivity = new Intent(getBaseContext(),
						MovieEditor.class);
				startActivity(editorActivity);
			}
		});
	}

	@Override
	protected void onStart() {
		super.onStart();
		fillForm();
	}

	private TextView getFieldByName(String fieldName, String preffix){
		int fieldId = 0;
		TextView field = null;
		try {
			fieldId = R.id.class.getField(
					preffix + fieldName).getInt(null);
		} catch (IllegalArgumentException e) {
		} catch (SecurityException e) {
		} catch (IllegalAccessException e) {
		} catch (NoSuchFieldException e) {
		}
		if (fieldId > 0) {
			field = (TextView) findViewById(fieldId);
		}
		return field;
	}	

	private void fillForm() {
		Map<String,String> columns = null;
		columns = MovieCatalog.db.getMovie(MovieList.mCurId);
		@SuppressWarnings("rawtypes")
		Set set = columns.entrySet();
		@SuppressWarnings("rawtypes")
		Iterator it = set.iterator();
		
		while(it.hasNext()){
			@SuppressWarnings("unchecked")
			Map.Entry<String, String> entry = (Entry<String, String>)it.next();
			String key = entry.getKey();
			TextView field = this.getFieldByName(key, "view_");
			if (field != null) {
				field.setText(entry.getValue());
			}
		}
	}
}
